using MrPP.Common;
using MrPP.UX;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace MrPP.SerriedCabinet
{
    public class ShelveView : ShelveAbstract
    {

        [SerializeField]
        private ShelveModel _model = null;

        [SerializeField]
        private Button _play;
        [SerializeField]
        private Button _stop;

      //  [SerializeField]
      //  private ShelveBinding _binding;
        [SerializeField]
        private Book _book;

        [SerializeField]
        private TextMeshPro _all;

        [SerializeField]
        private TextMeshPro _shelve;

        [SerializeField]
        private TextMeshPro _last;


        [SerializeField]
        private TextMeshPro _location;
        // Start is called before the first frame update
        void Awake()
        {
            /*
            _binding.onPage += delegate (int next)
            {
                refresh();
            };
            _binding.onScanList += doScanList;
            _binding.onRunning += doRunning;
            _binding.onLocation += doLocation;
            _binding.onBinding += doBinding;*/
        }
        void Start()
        {

            doLocation(model.getLocation());
            doRunning(model.getRunning());
            refresh();
        }

        private void doBinding(Dictionary<string, BoxLocation> map)
        {
            refresh();
        }

        private void doLocation(BoxLocation location)
        {
            if (location.box == -1)
            {
                _location.text = "请选择需要绑定的柜体";
            }
            else
            {
                _location.text = location.ToString();
            }


        }

        private void doRunning(bool running)
        {
            if (running)
            {
                _play.state = Button.State.Disable;
                _stop.state = Button.State.Enabled;
                if (QRCodeManager.IsInitialized)
                {
                    QRCodeManager.Instance.open();

                    _model.open();
                }
            }
            else
            {

                _play.state = Button.State.Enabled;
                _stop.state = Button.State.Disable;
                if (QRCodeManager.IsInitialized)
                {
                    QRCodeManager.Instance.close();
                    _model.close();
                }
            }
            _play.refresh();
            _stop.refresh();
        }

        private void doScanList(string[] obj)
        {

            refresh();
        }

        private void refresh()
        {
            Dictionary<string, BoxLocation> map = model.getBinding();

            string[] list = model.getScanList();
            List<string> show = new List<string>();
            int shelve = 0;
            int last = 0;
            foreach (var sn in list)
            {
                if (map.ContainsKey(sn))
                {
                    var location = map[sn];
                    string item = sn + ":(" + location.ToString() + ")";
                    show.Add(item);
                    shelve++;
                }
                else
                {
                    string item = sn + ":[待绑定]";
                    show.Add(item);
                    last++;
                }
            }
            _shelve.text = shelve.ToString();
            _last.text = last.ToString();
            _all.text = model.getScanList().Length.ToString();
            _book.page = model.getPage();

            _book.setItems(show);
            _book.refresh();

        }

        public override void onPage(int value)
        {
            refresh();
        }

        public override void onScanList(string[] list)
        {
            doScanList(list);
        }

        public override void onLocation(BoxLocation location)
        {
            doLocation(location);
        }

        public override void onRunning(bool value)
        {
            doRunning(value);
        }

        public override void onBinding(Dictionary<string, BoxLocation> data)
        {

            refresh();
        }


    }
}